package org.lanqiao.controller.notice;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.lanqiao.admins.Administrator;
import org.lanqiao.config.entity.ResponseData;
import org.lanqiao.notice.INoticeService;
import org.lanqiao.notices.Notice;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage/notice")
@Api(tags = "操作公告")
public class NoticeController {
    @Reference
    private INoticeService iNoticeService;
    @GetMapping("/noticeList")
    //此方法用来获取所有公告列表,不需要传参数
    @ApiOperation(value="公告列表",notes="获取所有公告")
    public ResponseData<Notice> noticeList(){
        List<Notice> noticeList = iNoticeService.noticeList();
        if(noticeList!= null){
            return ResponseData.success().putDataVule("noticelist",noticeList);
        }else{
            return ResponseData.serverInternalError();
        }
    }

    @PostMapping("/noticeQuery")
    @ApiOperation(value="条件查询公告列表",notes="根据条件查询相应的公告")
    @ApiImplicitParam(value = "查询条件" ,name="queryMap" ,required = false,dataType = "Map",paramType = "body")
    public ResponseData<Notice> noticelistBycondition(@RequestBody Map<String ,Object> queryMap){
        List<Notice> noticeList =iNoticeService.noticeListByCondition(queryMap);
        if(noticeList!= null){
            return ResponseData.success().putDataVule("noticelist",noticeList);
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法分页查询所有公告
    //param
    //    1:pagenum 当前页数
    //    2:pagesize 每页有多少数据
    @GetMapping("/noticePage")
    @ApiOperation(value = "分页查询",notes = "分页查询所有公告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页数",name = "pagenum",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pagesize",required = false,dataType = "Integer",paramType = "query")
    })
    public ResponseData<Notice> noticePage(@RequestParam  Integer pagenum,@RequestParam Integer pagesize){
        IPage<Notice> noticePage =  iNoticeService.noticePage(pagenum,pagesize);
        return ResponseData.success().putDataVule("totla",noticePage.getTotal())
                .putDataVule("pages",noticePage.getPages())
                .putDataVule("brandList",noticePage.getRecords());

    }

    //此方法根据条件分页查询所有管理员
    //param
    //    1:pagenum 当前页数
    //    2:pagesize 每页有多少数据
    //    3:queryMap 存放查询条件的map
    //           querymap中的参数--1:title：根据公告标题进行模糊查询
    //                           2:starttime:查询公告更新时间的范围(起始时间)
    //                            3:endtime:查询公告更新时间的范围(结束时间)
    @PostMapping("/noticePageByCondition")
    @ApiOperation(value = "分页条件查询",notes = "根据条件分页查询公告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页数",name = "pagenum",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pagesize",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "查询条件",name = "queryMap",required = false,dataType = "Map",paramType = "body")
    })
    public  ResponseData<Notice> brandPageQuery(@RequestParam  Integer pagenum,@RequestParam Integer pagesize,@RequestBody Map<String,Object> queryMap){
        IPage<Notice> noticePage =  iNoticeService.noticePageByCondition(pagenum,pagesize,queryMap);
        return ResponseData.success().putDataVule("totla",noticePage.getTotal())
                .putDataVule("pages",noticePage.getPages())
                .putDataVule("brandList",noticePage.getRecords());
    }

    //此方法用来增加公告
    //param
    //    notice:对应数据库中的ws_notice
    //说明:传notice的数据可以不用传id,因为id自增,成功插入则返回1
    @PostMapping("/addNotice")
    @ApiOperation(value="增加公告",notes="增加相应的公告信息")
    @ApiImplicitParam(value = "增加公告信息" ,name="notice" ,required = false,dataType = "Notice",paramType = "body")
    public ResponseData<Notice> addNotice(@RequestBody Notice notice){
        Integer res = iNoticeService.addNotice(notice);
        if(res == 1){
            return ResponseData.success();
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法用来更新公告信息
    //param
    //    notice:对应数据库中的ws_notice
    //说明:成功更新返回1
    @PostMapping("/updateNotice")
    @ApiOperation(value="更新某个公告",notes="更新某个公告的信息")
    @ApiImplicitParam(value = "更新公告信息" ,name="notice" ,required = false,dataType = "Notice",paramType = "body")
    public ResponseData<Notice> updateNotice(@RequestBody Notice notice){
        Integer res = iNoticeService.updateNotice(notice);
        if(res == 1){
            return ResponseData.success();
        }else{
            return ResponseData.serverInternalError();
        }
    }

    //此方法用来删除公告
    //param
    //    id:对应数据库中的ws_notice中的id
    //说明:成功删除返回1
    @GetMapping("/deleteNotice")
    @ApiOperation(value="删除某个公告",notes="根据id删除某个公告")
    @ApiImplicitParam(value = "删除公告" ,name="id" ,required = false,dataType = "Integer",paramType = "query")
    public ResponseData<Notice> deleteNotice(@RequestParam Integer id) {
        Integer res = iNoticeService.deleteNoticeById(id);
        if (res == 1) {
            return ResponseData.success();
        } else {
            return ResponseData.serverInternalError();
        }
    }
}
